﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class right_notice : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["username"]) != "")
        {
            panellogin.Visible = false;
            panellogout.Visible = true;
            login_UserName.Text = "欢迎回来，" + Convert.ToString(Session["username"]) + "！";
        }
        else if ((Request.Cookies["AutoLoginCode"]!=null)&&(Request.Cookies["UserNameCode"]!=null))
        {
            //取到连接字符串
            string propertiesPath = Server.MapPath("mysqldb.properties");
            mytools mt = new mytools();
            string ConnString = mt.getMysqlConn(propertiesPath);

            //打开连接
            MySqlConnection myConnection = new MySqlConnection(ConnString);
            myConnection.Open();

            MySqlDataAdapter da = new MySqlDataAdapter("select * from user_info where username='" + Request.Cookies["UserNameCode"].Value + "'", myConnection);
            DataSet ds = new DataSet();
            da.Fill(ds, "user_detail");
            DataRowView rowview = ds.Tables["user_detail"].DefaultView[0];

            string usernamecoo = rowview["username"].ToString();
            string pwdmd5coo=rowview["userpassword"].ToString();

            string autocode = usernamecoo + pwdmd5coo;
            autocode = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(autocode, "md5");

            if (String.Compare(Request.Cookies["AutoLoginCode"].Value, autocode, true) == 0)
            {
                panellogin.Visible = false;
                panellogout.Visible = true;
                login_UserName.Text = "欢迎回来，" + Convert.ToString(Request.Cookies["UserNameCode"].Value) + "！";
                Session["username"] = Request.Cookies["UserNameCode"].Value;
            }
            myConnection.Close();
        }
        else
        {
            panellogin.Visible = true;
            panellogout.Visible = false;
        }
    }

    protected void userloginbutton_Click(object sender, EventArgs e)
    {
        //获取当前URL
        string currenturl = Request.Url.ToString();

        bool bl = isautologinornot.Checked;


        if (Request.Cookies["CheckCode"] == null)
        {
            lblMessage.Text = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            lblMessage.Visible = true;
            return;
        }

        if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
        {
            lblMessage.Text = "*验证码错误";
            lblMessage.Visible = true;
            return;
        }

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string pwd = userpassword.Text;
        string pwd_md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
        string logining = "select count(*) from user_info where username='" + username.Text + "' and userpassword='" + pwd_md5 + "'";
        MySqlCommand dap = new MySqlCommand(logining, myConnection);
        int count = Convert.ToInt32(dap.ExecuteScalar());
        if (count > 0)
        {
            Session["username"] = username.Text.ToString();

            login_UserName.Text = "登录成功！欢迎你，" + username.Text + "！";

            panellogin.Visible = false;
            panellogout.Visible = true;

            
            if (bl == true)
            {
                string autocode = username.Text + pwd_md5;
                autocode = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(autocode, "md5");
                //Response.Write("<script>alert('登录成功！欢迎你，'+'" + autocode + "'+'！');</script>");
                Response.Cookies.Add(new HttpCookie("UserNameCode", username.Text));
                Response.Cookies["UserNameCode"].Expires = DateTime.Now.AddDays(30);

                Response.Cookies.Add(new HttpCookie("AutoLoginCode", autocode));
                Response.Cookies["AutoLoginCode"].Expires = DateTime.Now.AddDays(30);

            }
            myConnection.Close();
            
            //刷新当前页
            Response.Write("<script lanuage=javascript>location='" + currenturl + "'</script>");
            //Response.Write("<script>alert('登录成功！欢迎你，'+'" + username.Text + "'+'！');location='index.aspx'</script>");
        }
        else
        {
            lblMessage.Text = "*用户名或者密码输入有误，请重新输入！";
            lblMessage.Visible = true;
            myConnection.Close();
            return;
            //Response.Write("<script>alert('用户名或者密码输入有误，请重新输入！');location='index.aspx'</script>");
        }
    }

    protected void userlogoutbutton_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["username"]) != "")
        {
            //获取当前URL
            string currenturl = Request.Url.ToString();

            Session["username"] = "";

            Response.Cookies["UserNameCode"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["AutoLoginCode"].Expires = DateTime.Now.AddDays(-1);

            panellogin.Visible = true;
            panellogout.Visible = false;
            lblMessage.Text = "";
            lblMessage.Visible = false;

            username.Text = "";
            userpassword.Text = "";
            txtCheckCode.Text = "";

            Response.Write("<script lanuage=javascript>location='" + currenturl + "'</script>");

        }


    }

}
